import { createRouter, createWebHistory } from 'vue-router'
import GraduationRequirementAchievementReport from '../views/GraduationRequirementAchievementReport.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/profile',
    redirect: '/dashboard/profile'
  },
  {
    path: '/dashboard',
    component: () => import('../views/Dashboard.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'DashboardRedirect',
        redirect: '/dashboard/profile'
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { requiresAuth: true }
      },
      {
        path: 'employers',
        name: 'Employers',
        component: () => import('../views/Employers.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'internship-units',
        name: 'InternshipUnits',
        component: () => import('../views/InternshipUnits.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'operation-logs',
        name: 'OperationLogs',
        component: () => import('../views/OperationLogs.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN'] }
      },
      // System Management
      {
        path: 'users',
        name: 'Users',
        component: () => import('../views/Users.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN'] }
      },
      {
        path: 'role-permissions',
        name: 'RolePermissions',
        component: () => import('../views/RolePermissions.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN'] }
      },
      // Basic Data
      {
        path: 'majors',
        name: 'Majors',
        component: () => import('../views/Majors.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'courses',
        name: 'Courses',
        component: () => import('../views/Courses.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'course-scores',
        name: 'CourseScores',
        component: () => import('../views/course/CourseScore.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'graduation-requirements',
        name: 'GraduationRequirements',
        component: () => import('../views/GraduationRequirements.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'indicators',
        name: 'Indicators',
        component: () => import('../views/Indicators.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      // Evaluation Management
      {
        path: 'course-support-matrices',
        name: 'CourseSupportMatrices',
        component: () => import('../views/CourseSupportMatrices.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'course-goal-achievements',
        name: 'CourseGoalAchievements',
        component: () => import('../views/CourseGoalAchievements.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'graduation-requirement-achievements',
        name: 'GraduationRequirementAchievements',
        component: () => import('../views/GraduationRequirementAchievements.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'evaluation-mechanisms',
        name: 'EvaluationMechanisms',
        component: () => import('../views/EvaluationMechanisms.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'teacher-evaluations',
        name: 'TeacherEvaluations',
        component: () => import('../views/TeacherEvaluations.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'graduate-self-evaluations',
        name: 'GraduateSelfEvaluations',
        component: () => import('../views/GraduateSelfEvaluations.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'employer-evaluations',
        name: 'EmployerEvaluationsPage',
        component: () => import('../views/EmployerEvaluations.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'internship-unit-evaluations',
        name: 'InternshipUnitEvaluationsPage',
        component: () => import('../views/InternshipUnitEvaluations.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER','GRADUATE'] }
      },
      {
        path: 'direct-evaluations',
        name: 'DirectEvaluations',
        component: () => import('../views/DirectEvaluations.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER', 'MAJOR_LEADER','GRADUATE'] }
      },
      {
        path: 'graduate-requirement-achievement',
        name: 'GraduateRequirementAchievement',
        component: () => import('../views/GraduateRequirementAchievement.vue'),
        meta: { requiresAuth: true, roles: ['SYSTEM_ADMIN', 'TEACHER', 'MAJOR_LEADER','GRADUATE'] }
      },
      {
        path: 'graduation-requirement-achievement-report',
        name: 'GraduationRequirementAchievementReport',
        component: GraduationRequirementAchievementReport,
        meta: {
          title: '毕业要求达成度报表',
          requiresAuth: true
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  console.log('路由跳转到:', to.path)
  const token = localStorage.getItem('token')
  const userRole = localStorage.getItem('userRole')

  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else if (to.meta.roles && !to.meta.roles.includes(userRole)) {
    next('/dashboard')
  } else {
    next()
  }
})

export default router 